﻿using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kalman.Extensions;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.IO;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Authorization;
using Org.BouncyCastle.Ocsp;
using Kalman.Security;
using Kalman.Enums;

namespace QuDao.Api
{
    /// <summary>
    /// 验证模型数据
    /// </summary>
    public class ValidateModelAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)//验证参数的合法性问题。返回错误信息
            {
                var sb = new StringBuilder();
                foreach (var item in context.ModelState.Values)
                {
                    foreach (var error in item.Errors)
                    {
                        sb.Append($"{ error.ErrorMessage}|");
                    }
                }

                context.Result = new BadRequestObjectResult(new { code = 400, msg = sb.ToString().TrimEnd('|') });
            }
        }

    }
}
